﻿using AutoMapper;
using B.S.DataBase.MyProject.Domain.wdx;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure;
using MediatR;
using MedicalPCTerminal.Write.API.Appcation.Commands.ResidentFilesCommand;

namespace MedicalPCTerminal.Write.API.Appcation.CommandHandlers.ResidentFilesCommandHandler
{
    public class AddResidentFilesCommandHandler : IRequestHandler<AddResidentFilesCommand, APIResult<object>>
    {
        private readonly SqlSugarContext sqlSugar;
        private readonly IMapper mapper;

        public AddResidentFilesCommandHandler(SqlSugarContext sqlSugar, IMapper mapper)
        {
            this.sqlSugar = sqlSugar;
            this.mapper = mapper;
        }

        public Task<APIResult<object>> Handle(AddResidentFilesCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            var model = mapper.Map<ResidentFiles>(request);
            var r = sqlSugar.db.Insertable(model).ExecuteCommand();
            if (r > 0)
            {
                result.Code = APIEnum.添加成功;
                result.Msg = APIEnum.添加成功.ToString();
                result.data = model;
            }
            else
            {
                result.Code = APIEnum.添加失败;
                result.Msg = APIEnum.添加失败.ToString();
                result.data = null;
            }
            return Task.FromResult(result);
        }
    }
}
